package concurrent.thread;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2021-05-09 15:01:00
 */
@Slf4j
public class ThreadLocalTest {

    @Test
    public void test01(){
        ThreadLocal<String> local = new ThreadLocal<>();
        local.set("Hello Lu");
        Thread thread = new Thread(() -> {
            log.info(local.get());
        });
        thread.setDaemon(false);
        thread.start();
        log.info(local.get());
    }

    @Test
    public void test02(){
        ThreadLocal<String> local = new InheritableThreadLocal<>();
        local.set("Hello Lu");
        Thread thread = new Thread(() -> {
            log.info(local.get());
        });
        thread.setDaemon(false);
        thread.start();
        log.info(local.get());
    }

    private volatile int count = 0;

    @Test
    public void test03() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int j = 0; j < 10; j++) {
                    count ++;
                }
            }).start();
        }
        Thread.sleep(5000);
        log.info(String.valueOf(count));
    }

}
